<?php
/**
 * Controller for actions (in admin area) of member module
 *  
 * @version 2012-10-10
 * @package member
 * @subpackage controller
 * @category Member_Controller_Admin
 */

class Keys_Controller_Admin extends Controller
{
	/**
	 * Default action
	 *
	 * @version 2013-01-18
	 * @param Request $req
	 * @return View
     * @access public 
	 */	
	public function processIndex($req)
	{
		return $this->processKeysMembers($req);
	}

	/**
	 * Member
	 * This function shows the list of members.
	 * This function forms a query to the database with the help of a block of filters and returns an object VIEW that contains the array 'members' with data
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return View
     * @access public 
	 */
	public function processKeysMembers($req) {

		// Here local values are declared 
		$view = $this->getView("admin_member_keys");
		$uuid = $req->get('uuid');
		$dbMembers = Db::getModel('member_accounts');
		$dbDataKeys = Db::getModel('data_keys');
		$dbCategories = Db::getModel('categories');
		$user = $dbMembers->find($uuid);
		m('members');
		
		if(!$user){$req->forward("/admin");}
		
		
		$url = new Url();
		$pager = new Pager($url,_cfg('global.pager'));
		$sorter = new Sorter($url);
		$sorterCfg = array(
			'id',
			'category_title',
        );
		
		// Here filter conditions are set up
        $filterCfg= array(
            'category_id' => '=',
        );
		$filter = new Filter('admin_member_keys', $filterCfg);
		
		$filter->process($req);
		$sorter->initUp($sorterCfg);
		$sort   = array($sorter->getOrder());
		$where = $filter->getCriteria();
		$view->setVar('fltInfo',$filter->getInfo());
		$rows = $dbDataKeys->getMemberKeyList($user['uuid'],$where,$pager,$sort);
		$tpl_tr = array('id'=>'tpl_tr','class'=>'hide');
		array_unshift($rows, $tpl_tr);
		$view->setVar('rows',$rows);
		
		$view->setVar('srtInfo', $sorter->getInfo());
		$view->setVar('categories',$dbCategories->getHash('title',array('status'=>'IN("active ")')));
		$view->setVar('user', $user);
		$view->setVar('pagerInfo',$pager->getInfo());
		return $view;
	}

	
	/**
	 * Block
	 * This function edits the status of a member in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processDelete($req){
		$url = new Url();
		$uuids = $req->getArray("uuid");
		$dbDataKeys = Db::getModel('data_keys');
		foreach($uuids as $uuid){
			$dbDataKeys->delete($uuid);
		}
		$req->forward($url->getUrl(),_fmt('ok'));
	}
	
	/**
	 * MemberAjaxAction
	 * This function edits the status of a member in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processAjaxKeysAction($req){
		$dbDataKeys = Db::getModel('data_keys');
		
		$uuid = $req->get('uuid');
			$method = $req->get('method');
			if($method == "delete"){
				echo $dbDataKeys->delete($uuid);
			}
		exit;
	}
	
	 /**
	 * AjaxAddMember
	 * This function add member in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processAjaxAddKey($req){
		$dbKeys = Db::getModel('data_keys');
		$data = $req->getArray('data');
		$rules = array();
		
		$fields['data[title]'] = array('title'=>'Title', 'def' => 'required');
		$fields['data[login]'] = array('title'=>'Login', 'def' => 'required');
		$fields['data[password]'] = array('title'=>'Password', 'def' => 'required');
		$fields['data[host]'] = array('title'=>'Host', 'def' => 'required');
		
		$val = new Validator($fields);
		if(!$val->isValid($req->getArgs())){
			$result['status'] = "error";
			foreach($val->getErrors() as $e)
				$result['msg'] .= $e."<br>";
		}
		else{
			if($data['uuid']){
				$request = $dbKeys->updateKey($data);
				$result['id'] = $request['data_keys']['id'];
				$result['param_1'] = $request['data_keys']['param_1'];
				$result['param_2'] = $request['data_keys']['param_2'];
				$result['param_3'] = $request['data_keys']['param_3'];
				$result['param_4'] = $request['data_keys']['param_4'];
				$result['param_5'] = $request['data_keys']['param_5'];
				$result['param_6'] = $request['data_keys']['param_6'];
				$result['param_7'] = $request['data_keys']['param_7'];
				$result['category_title'] = $request['data_keys']['category_title'];
				
				$result['msg'] .= _fmt("ok");
				$result['status'] = "ok_update";
			}else{
				$request = $dbKeys->addKey($data);
				$result['id'] = $request['id'];
				$result['param_1'] = $request['data_keys']['param_1'];
				$result['param_2'] = $request['data_keys']['param_2'];
				$result['param_3'] = $request['data_keys']['param_3'];
				$result['param_4'] = $request['data_keys']['param_4'];
				$result['param_5'] = $request['data_keys']['param_5'];
				$result['param_6'] = $request['data_keys']['param_6'];
				$result['param_7'] = $request['data_keys']['param_7'];
				$result['category_title'] = $request['data_keys']['category_title'];
				$result['msg'] .= _fmt("ok");
				$result['status'] = "ok";
			}
		}
		echo json_encode($result);
		exit;
	}
	
	/**
	 * AjaxMemberInfo
	 * This function shows the list of members.
	 * This function forms a query to the database with the help of a block of filters and returns an object VIEW that contains the array 'members' with data
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return View
     * @access public 
	 */
	public function processAjaxInfoMemberKey($req) {
		// Here local values are declared 
		$dbDataKeys = Db::getModel('data_keys');
		$uuid = $req->get('uuid');
		$membmer_uuid = $req->get('member_uuid');
		$result = $dbDataKeys->getKey(array("member_uuid"=>$membmer_uuid,"id"=>(int)$uuid));
		echo json_encode($result);
		exit;
	}
}